package com.useeinfo.asg.open.web;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.AddressBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by GeniusJ on 2017-05-09.
 */
@Controller
@RequestMapping("/mobile/address")
public class AddressManageAction {

    @Autowired
    private AddressBiz addressBiz;


    /**
     * 查询市
     * @param request
     * */
    @RequestMapping("/city")
    @ResponseBody
    public JSONObject cityJson(HttpServletRequest request) {
        JSONObject retJson = CommUtil.jsonResult(true);
        QueryParam queryParam = new QueryParam(-1,-1);
        queryParam.getSqlMap().put("level","2");

        retJson.put("contents",addressBiz.findJSONList(queryParam));
        return retJson;

    }

    /**
     * 查询当前市下面的区
     * @param request (parentId 市级Id,当他为空时查询所有的区)
     * */
    @RequestMapping("/area")
    @ResponseBody
    public JSONObject areaJson(HttpServletRequest request) {
        JSONObject retJson = CommUtil.jsonResult(true);

        String parentId = request.getParameter("parentId");

        QueryParam queryParam = new QueryParam(-1,-1);

        queryParam.setSqlOrder(" order by s.sequence asc ");

        if(CommUtil.null2Long(parentId)<=0) {
            queryParam.getSqlMap().put("level","3");
        }
        else {
            queryParam.getSqlMap().put("parent_id",parentId);
        }


        retJson.put("contents",addressBiz.findJSONList(queryParam));
        return retJson;
    }

    /**
     * 查询当前区下面的街道
     * @param request (parentId 区级ID)
     * */
    @RequestMapping("/street")
    @ResponseBody
    public JSONObject streetJson(HttpServletRequest request) {
        JSONObject retJson = CommUtil.jsonResult(true);

        String parentId = request.getParameter("parentId");

        QueryParam queryParam = new QueryParam(-1,-1);
        queryParam.getSqlMap().put("level","4");
        queryParam.getSqlMap().put("parent_id",parentId);

        retJson.put("contents",addressBiz.findJSONList(queryParam));
        return retJson;
    }
}
